{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "73bd968b-d970-4a05-94ef-4e7abf990827",
   "metadata": {},
   "source": [
    "Chapter 18\n",
    "\n",
    "# 偏积分\n",
    "Book_3《数学要素》 | 鸢尾花书：从加减乘除到机器学习 (第二版)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "def700bf-df83-4e28-ab65-f840c63791fe",
   "metadata": {},
   "source": [
    "这段代码计算了二维高斯函数\n",
    "\n",
    "$$\n",
    "f(x, y) = e^{-x^2 - y^2}\n",
    "$$\n",
    "\n",
    "在两个独立方向上的偏积分，从而将原始的二维函数简化为一维表达形式。\n",
    "\n",
    "1. **对 $y$ 的积分**：首先，代码对 $f(x, y)$ 在 $y \\in (-\\infty, \\infty)$ 上进行积分，结果为\n",
    "\n",
    "   $$\n",
    "   \\int_{-\\infty}^{\\infty} e^{-x^2 - y^2} \\, dy = \\sqrt{\\pi} e^{-x^2}\n",
    "   $$\n",
    "\n",
    "   该结果表示在 $y$ 方向上的累积效应，产生一个关于 $x$ 的一维高斯函数形式。这表明在 $y$ 上积分后，函数的概率密度沿 $x$ 方向保持高斯分布。\n",
    "\n",
    "2. **对 $x$ 的积分**：然后，代码对 $f(x, y)$ 在 $x \\in (-\\infty, \\infty)$ 上进行积分，结果为\n",
    "\n",
    "   $$\n",
    "   \\int_{-\\infty}^{\\infty} e^{-x^2 - y^2} \\, dx = \\sqrt{\\pi} e^{-y^2}\n",
    "   $$\n",
    "\n",
    "   这给出了 $x$ 方向上的累积效应，得到关于 $y$ 的一维高斯函数形式。说明在 $x$ 上积分后，函数的概率密度沿 $y$ 方向仍然保持高斯分布。\n",
    "\n",
    "通过这些偏积分，代码展示了二维高斯函数在单个方向上积分后，如何保留另一变量的高斯分布性质。这些计算是多维高斯函数性质的关键，揭示了高斯分布的独立性和累积行为。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "29f75535-48d4-4a18-a5c7-bbcd185ac65c",
   "metadata": {},
   "source": [
    "## 导入包"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "3ae4d8b0-4681-4672-852f-b92fe862f3d8",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sympy.abc import x, y, s, t  # 导入符号变量 x, y, s, t\n",
    "from sympy import *  # 导入符号计算库"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "5e041c3d-f2cf-4a75-af63-5ae4c8fda321",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle e^{- x^{2} - y^{2}}$"
      ],
      "text/plain": [
       "exp(-x**2 - y**2)"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f_xy = exp(- x**2 - y**2)  # 定义函数 f(x, y) = exp(-x^2 - y^2)\n",
    "f_xy"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "89fb2d8a-6291-4732-898e-52f97d9a2c29",
   "metadata": {},
   "source": [
    "## 对 y 进行偏积分"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "66d8b977-dfe7-4622-a026-1057185dcb9c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\sqrt{\\pi} e^{- x^{2}}$"
      ],
      "text/plain": [
       "sqrt(pi)*exp(-x**2)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f_x_partial_integrate = integrate(f_xy, (y, -oo, oo))  # 计算 f(x, y) 在 y 方向上的偏积分\n",
    "f_x_partial_integrate"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "62f508d4-c704-4962-afb2-cc169d5e819f",
   "metadata": {},
   "source": [
    "## 对 x 进行偏积分"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "0b01b449-3ff1-4d8e-9e40-21af30ab4944",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\sqrt{\\pi} e^{- y^{2}}$"
      ],
      "text/plain": [
       "sqrt(pi)*exp(-y**2)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f_y_partial_integrate = integrate(f_xy, (x, -oo, oo))  # 计算 f(x, y) 在 x 方向上的偏积分\n",
    "f_y_partial_integrate"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "85a80909-2aac-49ed-bb7a-f8cc6b80ee7d",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ecd322f4-f919-4be2-adc3-69d28ef25e69",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
